package web;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Random;

import vo.Source;
import vo.SourceCategory;
import vo.User;
import business.SourceCategoryService;
import business.SourceService;
import business.UserService;
import business.UserTopicService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class SourceAction implements Preparable {
	private SourceService service;
	private UserService userService;
	private UserTopicService userTopicService;
	private SourceCategoryService sourceCategoryService;
	private List<Source> sources;
	private ArrayList<String> delIds = new ArrayList<String>();
	private String name;
	private String type;
	private String entry;
	private int curPage;
	private int totalPage;
	private int pageSize = 5;
	private User curUser;

	public String listSource() {
		//取出检索条件
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		name = (String) session.get("sourceName");
		type = (String) session.get("sourceType");
		entry = (String) session.get("sourceEntry");

		System.out.println("list source");
		System.out.println("name:" + name);
		System.out.println("type:" + type);
		System.out.println("entry:" + entry);

		boolean isNameNull = (name == null) || (name.length() == 0);
		boolean isTypeNull = (type == null) || (type.length() == 0);
		boolean isEntyNull = (entry == null) || (entry.length() == 0);

		int totalCount = 0;
		if (curPage == 0)
			curPage = 1;

		// 如果有入口地址，那么唯一检索
		if (!isEntyNull) {
			curPage = 1;
			totalPage = 1;
			this.sources=new ArrayList<Source>();
			this.sources.add(service.getByEntry(entry));
		} else {
			// 没有任何检索条件
			if (isNameNull && isTypeNull) {
				totalCount = (int) service.getTotalCount();
				if (totalCount % pageSize == 0)
					totalPage = totalCount / pageSize;
				else
					totalPage = totalCount / pageSize + 1;
				this.sources = service.getAllSources((curPage - 1) * pageSize, pageSize);
			}
			// 只有名称检索条件
			else if (!isNameNull && isTypeNull) {
				totalCount = (int) service.getTotalCountByName(name);
				if (totalCount % pageSize == 0)
					totalPage = totalCount / pageSize;
				else
					totalPage = totalCount / pageSize + 1;
				this.sources = service.getByName((curPage - 1) * pageSize, pageSize, name);
			}
			// 只有type检索条件
			else if (isNameNull && !isTypeNull) {
				totalCount = (int) service.getTotalCountByType(type);
				if (totalCount % pageSize == 0)
					totalPage = totalCount / pageSize;
				else
					totalPage = totalCount / pageSize + 1;
				this.sources = service.getByType((curPage - 1) * pageSize, pageSize, type);
			}
			// name和type都是检索条件
			else {
				totalCount = (int) service.getTotalCountByNameType(name, type);
				if (totalCount % pageSize == 0)
					totalPage = totalCount / pageSize;
				else
					totalPage = totalCount / pageSize + 1;
				this.sources = service.getByNameType((curPage - 1) * pageSize, pageSize, name, type);
			}
		}
		System.out.println("sources count:" + this.sources.size());
		System.out.println("curPage:" + curPage);
		System.out.println("totalCount:" + totalCount);

		return Action.SUCCESS;
	}

	public String searchSource() {
		System.out.println("search source");
		System.out.println("name:" + name);
		System.out.println("type:" + type);
		System.out.println("entry:" + entry);
		

		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		//清除旧的检索条件
		session.remove("sourceName");
		session.remove("sourceType");
		session.remove("sourceEntry");

		//保存当前的检索条件
		session.put("sourceName", name);
		session.put("sourceType", type);
		session.put("sourceEntry", entry);

		return "searchSource";
	}

	public String addSource() {
		service.addSource(null);
		return listSource();
	}

	public String deleteSource() {
		System.out.println("delete source:");
		for (int i = 0; i < this.delIds.size(); i++) {
			System.out.println("deleted ids：" + delIds.get(i));
			long id = Long.parseLong(delIds.get(i));
			service.delete(id);

		}
		return "sourceDeleted";
	}

	public String saveSource() {
		System.out.println("form save");
		System.out.println("name:" + name);
		System.out.println("type:" + type);
		System.out.println("entry:" + entry);
		System.out.println(new Random().nextInt());

		Source source = new Source();
		source.setName(name);
		source.setType(type);
		source.setEntry(entry);
		service.addSource(source);
		return "saveSource";
	}

	public List<Source> getSources() {
		return sources;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
		System.out.println("curUser.getLoginName():" + curUser.getLoginName());
	}

	public void setSourceService(SourceService service) {
		this.service = service;
	}

	public ArrayList<String> getDelIds() {
		return delIds;
	}

	public void setDelIds(ArrayList<String> delIds) {
		this.delIds = delIds;
	}

	public void setService(SourceService service) {
		this.service = service;
	}

	public void setSources(List<Source> sources) {
		this.sources = sources;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getEntry() {
		return entry;
	}

	public void setEntry(String entry) {
		this.entry = entry;
	}

	public void setSourceCategoryService(SourceCategoryService sourceCategoryService) {
		this.sourceCategoryService = sourceCategoryService;
	}

	public void setSourceCategoryservice(SourceCategoryService sourceCategoryService) {
		this.sourceCategoryService = sourceCategoryService;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setUserTopicService(UserTopicService userTopicService) {
		this.userTopicService = userTopicService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

}